1703E - Mirror Grid - CodeForces Solution


implementation *1200

Please click on ads to support us..

Python Code:

t = int(input())
for i in range(t):
    n = int(input())
    a = []
    op = 0
    for i in range(n):
        a.append(list(map(int, list(input()))))

    for i in range(n - 1):
        for j in range(i, n - 1):
            one = zero = 0

            if a[i][j]:
                one += 1
            else:
                zero += 1
            
            if a[j][n - i - 1]:
                one += 1
            else:
                zero += 1
            
            if a[n - i - 1][n - j - 1]:
                one += 1
            else:
                zero += 1
            
            if a[n - j - 1][i]:
                one += 1
            else:
                zero += 1
            
            if one >= zero:
                op += zero
                a[i][j] = 1
                a[j][n - i - 1] = 1
                a[n - i - 1][n - j - 1] = 1
                a[n - j - 1][i] = 1
            else:
                op += one
                a[i][j] = 0
                a[j][n - i - 1] = 0
                a[n - i - 1][n - j - 1] = 0
                a[n - j - 1][i] = 0
    
    print(op)

C++ Code:

//By TOP
#include <iostream>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <list>
#include <unordered_set>
#include <cmath>
#include <bits/stdc++.h>
#include <deque>
#include <vector>
#include <utility>
using namespace std;
#define int long long
typedef pair<int,int> PII;
#define OO 0x3f3f3f3f
#define rd read()
#define mem(a,b) memset(a,b,sizeof a)
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define v1 priority_queue<int,vector<int>,greater<int> > v 
#define v2 priority_queue<int,vector<int>,less<int> > v 
#define debug(a) cout << a << "\n"
const int N = 2000010;
int a[N];
int n;
int tr[N];
/*void build(int u , int l , int r)
{
	tr[u] = {l , r};
	if(l == r)
	{
		return ;
	}
	int mid = l + r >> 1;
	build(u << 1 , l , mid);
	build(u << 1 | 1 , mid + 1  ,r);
}*/
/*int query(int u , int l , int r)
{
	if(tr[u].l >= l && tr[u].r <= r)
	{
		return tr[u].v;
	}
	int mid = tr[u].l + tr[u].r >> 1;
	int v = 0;
	if(l <= mid)
	{
		v.query(u << 1 , l , r);
	}
	if(r > mid)
	{
		v.query(u << 1 | 1 , l , r);
	}
	return v;
	
}*/
int lowbit(int x)
{
	return x & -x;
}
void add(int x,int c)
{
	for(int i = x ; i <= n ; i += lowbit(i) )
	{
		tr[i] += c;
	}
}
int query(int x)
{
	int res = 0;
	
	for(int i = x ; i ; i -= lowbit(i))
	{
		res += tr[i];
	}
	return res;
	
}
inline int read()
 {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	return x * f;
}
char c[1000][1000];

void solve()
{
	int n;
	cin >> n;
	for(int i = 1 ; i <= n ; i ++ )
	{
		for(int j = 1 ; j <= n ; j ++ )
		{
			cin >> c[i][j];
		}
	}
	int ans = 0;
	
	for(int i = 1 ; i <= (n + 1) / 2 ; i ++ )
	{
		for(int j = 1 ; j <= n / 2 ; j ++ )
		{
			int res = 0;
			res += c[i][j] - '0';
			res += c[n + 1 - j][i] - '0';
			res += c[n + 1 - i][n + 1 - j] - '0';
			res += c[j][n + 1 - i] - '0';
			res = min(res , 4 - res);
			ans += res;
			
		}
	}
	cout << ans << "\n";
	
}
signed main()
{
	ios;
	
	int t = 1;
	cin >> t;
	while(t -- )
	{
		solve();
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II